package com.ruibang.glass.produce.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruibang.glass.produce.constant.Constants;
import com.ruibang.glass.produce.domain.req.BcpCleanRoomRecordReq;
import com.ruibang.glass.produce.entity.DefectDirectly;
import com.ruibang.glass.produce.entity.ProduceBcpCleanRoomRecord;
import com.ruibang.glass.produce.mapper.ProduceBcpCleanRoomRecordMapper;
import com.ruibang.glass.produce.service.ProduceBcpCleanRoomRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.produce.util.ExcelStyleUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import rx.annotations.Beta;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 生产-洁净室-半成品洁净室环境记录 服务实现类
 * </p>
 *
 * @author 宋锦康
 * @since 2024-03-11
 */
@Slf4j
@Service
public class ProduceBcpCleanRoomRecordServiceImpl extends ServiceImpl<ProduceBcpCleanRoomRecordMapper, ProduceBcpCleanRoomRecord> implements ProduceBcpCleanRoomRecordService {



    @Override
    public IPage<ProduceBcpCleanRoomRecord> pageQuery(BcpCleanRoomRecordReq bcpCleanRoomRecordReq) {
        QueryWrapper<ProduceBcpCleanRoomRecord> qw = new QueryWrapper<>();
        qw.lambda().eq(ProduceBcpCleanRoomRecord::getIsDelete, Constants.ZERO_STR);
        if (bcpCleanRoomRecordReq.getStartTime() != null && bcpCleanRoomRecordReq.getEndTime() != null) {
            qw.lambda().between(ProduceBcpCleanRoomRecord::getDate, bcpCleanRoomRecordReq.getStartTime(), bcpCleanRoomRecordReq.getEndTime());
        }
        return this.page(new Page<>(bcpCleanRoomRecordReq.getPageNo(), bcpCleanRoomRecordReq.getPageSize()), qw.lambda());

    }

    @Override
    public void delete(String id) {
        lambdaUpdate().eq(ProduceBcpCleanRoomRecord::getId, id).set(ProduceBcpCleanRoomRecord::getIsDelete, Constants.ONE_STR).update();
    }

    @Override
    public void export(BcpCleanRoomRecordReq bcpCleanRoomRecordReq, HttpServletResponse response) {
        bcpCleanRoomRecordReq.setPageNo(-1);
        bcpCleanRoomRecordReq.setPageSize(-1);
        IPage<ProduceBcpCleanRoomRecord> page = this.pageQuery(bcpCleanRoomRecordReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<ProduceBcpCleanRoomRecord> produceBcpCleanRoomRecordList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), ProduceBcpCleanRoomRecord.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(produceBcpCleanRoomRecordList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }
}
